package com.tenton.utils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @Date: 2021/2/7
 * @Author: Tenton
 * @Description: 文件下载
 */
public class FileUtil {

    public static void download(String fileName, HttpServletResponse response) throws IOException {
        // 发送给客户端的数据
        OutputStream outputStream = response.getOutputStream();
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        // 读取fileName
        bis = new BufferedInputStream(new FileInputStream(new File("./file/" + fileName)));
        int i = bis.read(buff);
        while (i != -1) {
            outputStream.write(buff, 0, buff.length);
            outputStream.flush();
            i = bis.read(buff);
        }
    }
}
